﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Interface;
using PPI.Client.Logic.Delegate;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities.Inspections;

namespace PPI.CRUD
{
    public partial class frmEditStructure : frmBase,IEditStructure
    {
        private EditStructurePresenter m_presenter;
        private List<EInspectionResult> m_inpectionResults;
        private EInspectionResult m_inspection;
        private List<EInspection> m_searchedInspections;
        private delegate void UpdateBarControlCallback(Int32 percentage);
        private delegate void UpdateMessageControlCallback(string message);

        public frmEditStructure()
        {
            InitializeComponent();
        }

        #region IEditStructure
        public List<EInspectionResult> InpectionResults
        {
            get { return m_inpectionResults; }
            set { m_inpectionResults = value; }
        }

        public EInspectionResult InspectionResult
        {
            get { return m_inspection; }
            set { m_inspection = value; }
        }

        public string MessageProgress
        {
            set { UpdateProcessMessage(value); }
        }

        public int ProcessProgress
        {
            set { UpdateProcessBar(value); }
        }

        public List<EInspection> SearchedInspections
        {
            get { return m_searchedInspections; }
            set { m_searchedInspections = value;   }
        }

        public void AddInspections()
        {
            crtlInspectionList.SetInspections(m_searchedInspections);
        }

        public void ShowNewAutodeterminate()
        {
            MessageBox.Show("Hello");
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Application.DoEvents();
                m_presenter.StartSearchInspection();

            }
            catch(Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            m_presenter.Save();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void frmEditStructure_Load(object sender, EventArgs e)
        {
            try
            {
                m_presenter = new EditStructurePresenter(this);
                if (m_inspection != null)
                    ctrlInspectionPoleDetailsParent.SetInspection(m_inspection.Inspection);

            }
            catch(Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void UpdateProcessBar(int percentage)
        {
            if (this.InvokeRequired)
            {
                UpdateBarControlCallback d = new UpdateBarControlCallback(UpdateProcessBar);
                this.Invoke(d, new object[] { percentage });
            }
            else
            {
                pbProcess.Value = percentage;
            }
            
        }

        private void UpdateProcessMessage(string message)
        {
            if (this.InvokeRequired)
            {
                UpdateMessageControlCallback d = new UpdateMessageControlCallback(UpdateProcessMessage);
                this.Invoke(d, new object[] { message });
            }
            else
            {
                lblMessageProgress.Text = message;
            }
        }

        private void crtlInspectionList_ClickedInspection(object sender, EventArgs e)
        {
            if(crtlInspectionList.GetSelectedInspection() != null)
                ctrlInspectionPoleDetailsChild.SetInspection(crtlInspectionList.GetSelectedInspection());
        }

        private void crtlInspectionList_ClickedAddInspection(object sender, EventArgs e)
        {
             m_presenter.AddInspection((Int32)crtlInspectionList.GetInspectionToAdd());
        }
    }
}
